package cd.safe.school.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cd.safe.school.R;

public class StudentDetailAdapter extends BaseAdapter {
	
	private Context context;
	private List<Map<String, Object>> lists;
	private Handler handler = new Handler();
	public static HashMap<Integer, Boolean> isSelected;

	public StudentDetailAdapter(Context context, List<Map<String, Object>> lists) {
		this.context = context;
		this.lists = lists;
		init();
	}

	public void init() {
		isSelected = new HashMap<Integer, Boolean>();
		for (int i = 0; i < lists.size(); i++) {
			isSelected.put(i, false);
		}
	}

	@Override
	public int getCount() {

		return lists.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.detail_item, null);
			holder.tv = (TextView) convertView.findViewById(R.id.student_name);
			holder.cb = (CheckBox) convertView
					.findViewById(R.id.student_search);
			convertView.setTag(holder);
		}

		else {
			holder = (ViewHolder) convertView.getTag();

		}

		holder.tv.setText((CharSequence) lists.get(position).get("name"));
		holder.cb.setChecked(isSelected.get(position));
		convertView.setTag("tag0");
		// holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		//
		// @Override
		// public void onCheckedChanged(CompoundButton buttonView,
		// boolean isChecked) {
		// isSelected.put(position, !isChecked);
		// notifyDataSetChanged();
		//
		// }
		// });

		// 从网络获取头像

		// new Thread(new Runnable() {
		//
		// @Override
		// public void run() {
		// String uri = (String) lists.get(position).get("img");
		// try {
		// URL url = new URL(uri);
		// URLConnection conn = url.openConnection();
		// InputStream in = conn.getInputStream();
		// final Bitmap bm = BitmapFactory.decodeStream(in);
		// handler.post(new Runnable() {
		//
		// @Override
		// public void run() {
		// log.setImageBitmap(bm);
		//
		// }
		// });
		//
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		//
		// }
		// }).start();

		return convertView;
	}

	public class ViewHolder {
		public TextView tv = null;
		public CheckBox cb = null;
	}

}
